package org.webrtc.yl.utils;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.webrtc.IceCandidate;
import org.webrtc.SessionDescription;

public class WebrtcJsonUtils {
    private static final String TAG = "WebrtcJsonUtils";

    public static JSONObject onMessage(String msg){
        JSONObject json = null;
        try {
            json = new JSONObject(msg);
        } catch (JSONException e) {
            Log.w(TAG, "onClientMessage: "+e.getMessage());
        }
        return json;
    }

    public static JSONObject confirmWebrtc(){
        JSONObject json = new JSONObject();
        //jsonPut(json, "sdp", sdp.description);
        jsonPut(json, "type", "confirmWebrtc");
        jsonPut(json, "msgType", "confirmWebrtc");
        return json;
    }

    public static JSONObject rejectWebrtc(){
        JSONObject json = new JSONObject();
        //jsonPut(json, "sdp", sdp.description);
        jsonPut(json, "type", "rejectWebrtc");
        jsonPut(json, "msgType", "rejectWebrtc");
        return json;
    }

    public static JSONObject sendOfferSdp(SessionDescription sdp){
        JSONObject json = new JSONObject();
        jsonPut(json, "type", "offer");
        jsonPut(json, "sdp", sdp.description);
        jsonPut(json, "msgType", "sdp");
        return json;
    }

    public static JSONObject sendAnswerSdp(SessionDescription sdp){
        JSONObject json = new JSONObject();
        jsonPut(json, "sdp", sdp.description);
        jsonPut(json, "type", "answer");
        jsonPut(json, "msgType", "sdp");
        return json;
    }

    public static JSONObject sendLocalIceCandidate(IceCandidate candidate) {
        JSONObject json = new JSONObject();
        jsonPut(json, "type", "candidate");
        jsonPut(json, "label", candidate.sdpMLineIndex);
        jsonPut(json, "id", candidate.sdpMid);
        jsonPut(json, "sdpMLineIndex", candidate.sdpMLineIndex);
        jsonPut(json, "sdpMid", candidate.sdpMid);
        jsonPut(json, "candidate", candidate.sdp);
        jsonPut(json, "msgType", "iceCandidate");
        return json;
    }

    public static JSONObject sendLocalIceCandidateRemovals(IceCandidate[] candidates) {
        JSONObject json = new JSONObject();
        jsonPut(json, "type", "remove-candidates");
        JSONArray jsonArray = new JSONArray();
        for (final IceCandidate candidate : candidates) {
            jsonArray.put(toJsonCandidate(candidate));
        }
        jsonPut(json, "candidates", jsonArray);
        return json;
    }

    private static JSONObject toJsonCandidate(final IceCandidate candidate) {
        JSONObject json = new JSONObject();
        jsonPut(json, "label", candidate.sdpMLineIndex);
        jsonPut(json, "id", candidate.sdpMid);
        jsonPut(json, "candidate", candidate.sdp);
        return json;
    }

    private static void jsonPut(JSONObject json, String key, Object value) {
        try {
            json.put(key, value);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    public static JSONObject sendResolution(int width,int height){
        JSONObject json = new JSONObject();
        jsonPut(json, "type", "resolution");
        jsonPut(json, "width", width);
        jsonPut(json, "height", height);
        return json;
    }
}
